<?php

	//
	// DATABASE STUFF
	//
	
	// Connect to the database
	$con = mysql_connect("localhost", "crystalmarsh2", "oJE33mc7Br") or die ('Error connecting to mysql');
	// Set the database we connect to or die with error
	$db = "crystalmarsh2_vegasvip4u";
	mysql_select_db($db,$con) or die("Could not select db $db");
	
	//
	// calendar STUFF
	//
	// Set the start date for the calendars
	$startdate = strtotime("last Thursday");
	
	// This is the object used for getting dates to print them out. It is based off the date inputted above on $startdate
	$datetime = date('Y-m-d',$startdate);

	// Next start the DateTime object basted off the date specified above
	$date = new DateTime($datetime);						
	
	// Get the month and month's name from the start date
	$monthname = $date->format('F');
	$monthnum = $date->format('n');
	
	$day = $date->format('j');
	
	// Get year
	$year = $date->format('Y');
	
	// This is our tool to priting out the next seven days in calendar from date specified
	function printcalendardates($date) {
		$return = "<tr><td id=calendarcontent>&nbsp;</td>";
		// Now that we have the date object in place (on line 13 header.php), lets show the following 7 days starting from the date specified on line 7 header.php
		for ($i = 1; $i <= 7; $i++) {
			// get the day to print
			$day = $date->format('j');
			// get the name of the day to print
			$dayname = $date->format('D');
			// get the month number
			$monthnum = $date->format('n');
			// print out the date
			$return .= "<td id=calendardates>$dayname $monthnum/$day</td>";
			// Now that we are done printing out that day. Add +1 day to the DateTime for the next day to be printed out
			$date->modify('+1 day');
		}
		$return .= "<td width=75px> </td></tr>";
		return $return;
	}

	function printcalendarcontent($page) {
		$venues = array("rain","playboy_las_vegas","ghostbar","moon","lounge","pearl","palms_place_pool","rehab");
		foreach ($venues as &$venue) {
			$return = "<tr><td id=calendarcontent><a href='/?p=reservations&venue=$venue'><img src='images/venues/$venue/logo.jpg'></a></td>";
			$datetime = date('Y-m-d');
			$date = new DateTime($datetime);
			for ($i = 1; $i <= 7; $i++) {
				$day = $date->format('j');
				$daycontent = mysql_query("SELECT venue,day,content FROM calendar WHERE day='$day' AND venue='$venue'") or die (mysql_error());
				while ($row = mysql_fetch_assoc($daycontent)) {
					$content = $row["content"];
					$return .= "
						<td id=calendarcontent>
							<a href='/?p=reservations&venue=$venue'>$content</a>
						</td>
					";
				}
				$date->modify('+1 day');
			}
		}
		$return .= "</tr>";
		return $return;
	}
	
	// TO BE ADDED
	// This is to print out all the content for the calendar by grabbing it all from the database based off calendar page that is up
	function printcalendar($page,$venuename) {
		$datetime = date('Y-m-d');
		$date = new DateTime($datetime);
		$monthname = $date->format('F');
		$year = $date->format('Y');
		$calendardates = printcalendardates($date);
		$calendarcontent = printcalendarcontent($page);
		echo "
			<table id=calendar>
				<tr>
					<td colspan=9>Event Calendar for $monthname, $year</td>
				</tr>
				$calendardates
				$calendarcontent
				<td> </td>
			</table>
		";
	}
	
	//
	// SESSION HANDLING
	//
	
	// start up the session handler for logging in
	session_start();
	// Log them out if they clicked Log Out
	if (isset($_GET['logout'])) {
		// Destroy Session to log them out
		session_destroy();
		// Set the page to go back home
		header('Location: index.php'); 
		// Finish the page reading since we sent ourselves back to home page (if you remove this exit line it will break it)
		exit; 
	}
	
	//
	// FORM VALIDATION
	//
	
	/**
	Validate an email address.
	Provide email address (raw input)
	Returns true if the email address has the email address format and the domain exists.
	*/
	function validEmail($email) {
		$isValid = true;
		$atIndex = strrpos($email, "@");
		if (is_bool($atIndex) && !$atIndex) { $isValid = false; }
		else {
			$domain = substr($email, $atIndex+1);
			$local = substr($email, 0, $atIndex);
			$localLen = strlen($local);
			$domainLen = strlen($domain);
			// local part length exceeded
			if ($localLen < 1 || $localLen > 64) { $isValid = false; }
			// domain part length exceeded
			else if ($domainLen < 1 || $domainLen > 255) { $isValid = false; }
			// local part starts or ends with '.'
			else if ($local[0] == '.' || $local[$localLen-1] == '.') { $isValid = false; }
			// local part has two consecutive dots
			else if (preg_match('/\\.\\./', $local)) { $isValid = false; }
			// character not valid in domain part
			else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) { $isValid = false; }
			// domain part has two consecutive dots
			else if (preg_match('/\\.\\./', $domain)) { $isValid = false; }
			else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',str_replace("\\\\","",$local))) {
				if (!preg_match('/^"(\\\\"|[^"])+"$/',str_replace("\\\\","",$local))) { $isValid = false; }
			}
			// domain not found in DNS
			if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) { $isValid = false; }
		}
		return $isValid;
	}
?>
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
<head>
	<!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
	<meta charset=utf-8 />
	<meta name="description" content="Your Personal Vegas Connection" /> 
	<meta name="robots" content="index, follow" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
 	<meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- CSS FILES -->
  <link rel="stylesheet" href="css/core.css" type="text/css">
	<link rel="stylesheet" href="css/main.css" type="text/css">
  <!-- Java scritps that speed up the page and help make it work on multiple browsers -->
  <script src="js/modernizr-1.7.min.js"></script>
	<script src="http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js"></script>
  <!-- Page Title (set in each page on top like index.php for home page -->
  <title><?php echo $title; ?></title>
  
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>
	<SCRIPT LANGUAGE="JavaScript">
		var n;
		var p;
		var p1;
		function ValidatePhone() {
			p=p1.value
			if (p.length==3) {
				pp=p;
				d4=p.indexOf('(')
				d5=p.indexOf(')')
				if(d4==-1){
					pp="("+pp;
				}
				if(d5==-1){
					pp=pp+")";
				}
				document.form.phonenum.value="";
				document.form.phonenum.value=pp;
			}
			if (p.length>3) {
				d1=p.indexOf('(')
				d2=p.indexOf(')')
				if (d2==-1){
					l30=p.length;
					p30=p.substring(0,4);
					p30=p30+")"
					p31=p.substring(4,l30);
					pp=p30+p31;
					document.form.phonenum.value="";
					document.form.phonenum.value=pp;
				}
				}
			if (p.length>5) {
				p11=p.substring(d1+1,d2);
				if (p11.length>3){
					p12=p11;
					l12=p12.length;
					l15=p.length
					//l12=l12-3
					p13=p11.substring(0,3);
					p14=p11.substring(3,l12);
					p15=p.substring(d2+1,l15);
					document.form.phonenum.value="";
					pp="("+p13+")"+p14+p15;
					document.form.phonenum.value=pp;
				}
				l16=p.length;
				p16=p.substring(d2+1,l16);
				l17=p16.length;
				if (l17>3&&p16.indexOf('-')==-1){
					p17=p.substring(d2+1,d2+4);
					p18=p.substring(d2+4,l16);
					p19=p.substring(0,d2+1);
					pp=p19+p17+"-"+p18;
					document.form.phonenum.value="";
					document.form.phonenum.value=pp;
				}
			}
			setTimeout(ValidatePhone,100)
		}
		function getIt(m){
			n=m.name;
			p1=m
			ValidatePhone()
		}
		<!--
		function isNumberKey(evt) {
			var charCode = (evt.which) ? evt.which : event.keyCode
			if (charCode > 31 && (charCode < 48 || charCode > 57)) {
				alert("You pressed a non-numerical key (meaning only numbers are allowed here)");
				return false;
			}
		}
		//-->
		<!-- 	
		function ValidateReservationForm() {
			// Make sure name is filled out
			if (document.form.name.length < 1) {
				alert("Name not filled out. Please put in your first and last name.");
				return false;
			}
			
			// Make sure the phone number is filled out
			if (document.form.phonenum.length < 10) {
				alert("Phone number not put in or too short. Must be 10 digits or longer.");
				return false;
			}
						
			// Check if we need to validate email
			var elength = document.form.email.value;
			if (elength.length > 0) {
				// Validate Email
				var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
				var emailAddress = document.form.email.value;
				var emailAddress2 = document.form.email2.value;
				if (emailAddress != emailAddress2) {
					alert("Email Addresses do not match");
					return false;
				}
				var valid = emailRegex.test(emailAddress);
				if (!valid) {
					alert("Invalid e-mail address "+emailAddress);
					return false;
				}
			}
			
			// Validate number of Guests
			var males = document.form.males.value;
			var females = document.form.females.value;
			var guests = males+females;
			if (guests < 1) {
				alert("You need to tell us how many males/females are a part of this.");
				return false;
			}

			alert("Thank you for reserving a night in Las Vegas with us! One of our VIP guest service team members will be contacting you shortly to confirm your request.");
			return true;
		}
		function ValidateRegisterForm() {
			var password = document.form.password.value;
			var password2 = document.form.password2.value;
			var plength = document.form.password.length;
			if (password != password2) {
				alert("Passwords do not match");
				return false;
			}
			if (password.length < 8) {
				alert("Passwords need to be over 7 characters long");
				return false;
			}
			
			var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
			var emailAddress = document.form.email.value;
			var emailAddress2 = document.form.email2.value;
			if (emailAddress != emailAddress2) {
				alert("Email Addresses do not match");
				return false;
			}
			var valid = emailRegex.test(emailAddress);
			if (!valid) {
				alert("Invalid e-mail address "+emailAddress);
				return false;
			}

			alert("Thank you for registering with us! Now whenever you want to reserve a date with us you won't have to fill out as much information.");
			return true;
		}
		function ValidateMembersLoginForm() {
			var password = document.form.password.value;
			var password2 = document.form.password2.value;
			var plength = document.form.password.length;
			if (password != password2) {
				alert("Passwords do not match");
				return false;
			}
			if (password.length < 8) {
				alert("Passwords need to be over 7 characters long");
				return false;
			}
			
			var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
			var emailAddress = document.form.email.value;
			var emailAddress2 = document.form.email2.value;
			if (emailAddress != emailAddress2) {
				alert("Email Addresses do not match");
				return false;
			}
			var valid = emailRegex.test(emailAddress);
			if (!valid) {
				alert("Invalid e-mail address "+emailAddress);
				return false;
			}

			alert("Welcome back!");
			return true;
		}
		//-->
	</script>
	<script language="javascript" type="text/javascript" src="js/datetimepicker.js">
	//Date Time Picker script- by TengYong Ng of http://www.rainforestnet.com
	//Script featured on JavaScript Kit (http://www.javascriptkit.com)
	//For this script, visit http://www.javascriptkit.com 
	</script>
</head>
<body>
	<!--[if lt IE 7 ]>
	<script src="js/libs/dd_belatedpng.js"></script>
	<script> DD_belatedPNG.fix('img, .jpg_bg');</script>
	<![endif]-->
	
	<!-- Wrapper for entire page (background and all) -->
	<div id="wrapper">
	  <!-- Navigation + Logo -->
		<nav>
		<!-- Logo -->
			<div class="logo">
			  <a href="?p=reservations">
			  <center><img src="../images/logo-header-1.jpg" width="80%" height="20%" border="0" alt="" usemap="#header_vegasvip_map_Map"></center></a>
			</div>
		<!-- Navigation -->
			<ul>
		  <center>
			<li><a href="/">Home</a></li> | 
			<li><a href="/?p=nightlife">Night Life</a></li> | 
			<li><a href="/?p=gentlemensclubs">Gentlemen's Clubs</a></li> | 
			<li><a href="/?p=poolparties">Pool Parties</a></li> | 
			<li><a href="/?p=reservations">Reservations</a></li> | 
			<li><a href="/?p=members">Members Area</a></li>
			<?php if (isset($_SESSION['email'])) { echo ' | <li><a href="?logout=1" title="Log Out">Log Out</a></li>'; } ?>
		  </center>
		</ul>
		<hr>
		</nav>
		<div id=content>